#include "Reactor.hpp"
#include "Listener.hpp"
#include "NetCal.hpp"
int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        LOG(Level::FATAL) << "Usage server and port";
        exit(USAGE_ERRO);
    }
    //业务层
    unique_ptr cal(make_unique<NetCal>());

    //协议层
    unique_ptr<Protocol> protocol(make_unique<Protocol>([&cal](Request req)->Response{
        return cal->Execute(req);
    }));

    //创建监听类
    uint16_t port = stoi(argv[1]);
    shared_ptr<Connection> conn(make_shared<Listen>(port));
    //注册方法
    conn->RegisterHandler([&protocol](string& out)->string{
        string ret;
        while(true)
        {
            string package;
            if(!protocol->DeCode(out,&package))
                break;
            ret+=protocol->Execute(package);
        }
        return ret;
    });

    //反应堆（连接管理）
    unique_ptr<Reactor> reactor(make_unique<Reactor>());
    reactor->AddConnection(conn);
    reactor->Start();

    return 0;
}
